<?php

/**
 * 添加资讯分类信息
 * @return string
 */
function addConCate(){
	$PdoMySQL=new PdoMySQL;
	$arr=$_POST;
	// $arr['aTime'] = date("Y-m-d H:i",time());
	if($PdoMySQL->add($arr,"bac_con_cate")){
		$mes="添加成功!<br/><a href='addConCate.php'>继续添加</a>|<a href='listConCate.php'>查看资讯分类列表</a>";
	}else{
		$mes="添加失败!<br/><a href='addConCate.php'>重新添加</a>";
	}
	return $mes;
}
/**
 * 得到所有的资讯分类信息
 * @return array
 */

function getConCateByPage($page,$pageSize=2){
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_con_cate";
    global $totalRows;
	$totalRows=count($PdoMySQL->find($tables,null,'*',null,null,null,null));
	global $totalPage;
	$totalPage=ceil($totalRows/$pageSize);
	if($page<1||$page==null||!is_numeric($page)){
		$page=1;
	}

	if($page>=$totalPage)$page=$totalPage;
	$offset=($page-1)*$pageSize;
	if($offset < 0){
		$offset = 0;
	}
	$rows=$PdoMySQL->find($tables,null,'*',null,null,null,array($offset,$pageSize));

	return $rows;

}

/**
 * 得到指定ID的资讯分类信息
 * @return array
 */
function findConCateById($id){
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_con_cate";
	$row = $PdoMySQL->findById($tables, $id, 'id, cName');
	return $row;
}
/**
 * 编辑资讯分类信息
 * @param int $id
 * @return string
 */
function editConCate($id){
	$arr=$_POST;
	// $arr['aTime'] = date("Y-m-d H:i",time());

	$PdoMySQL=new PdoMySQL;
	$table = "bac_con_cate";

	if($PdoMySQL->update($arr,$table, "id={$id}",'','')){
		$mes="编辑成功!<br/><a href='listConCate.php'>查看资讯分类列表</a>";
	}else{
		$mes="编辑失败!<br/><a href='listConCate.php'>请重新修改</a>";
	}
	return $mes;
}

/**
 * 删除指定id资讯分类
 * @param int $id
 * @return string
 */
function delConCate($id){
	$PdoMySQL=new PdoMySQL;
	$table = "bac_con_cate";
	
	if($PdoMySQL->delete($table,"id={$id}")){
		$mes="删除成功!<br/><a href='listConCate.php'>查看资讯分类列表</a>";
	}else{
		$mes="删除失败!<br/><a href='listConCate.php'>请重新删除</a>";
	}
	return $mes;
}

/**
 * 得到所有分类
 * @return array
 */
function getAllConCate(){
	$PdoMySQL=new PdoMySQL;
	$table = "bac_con_cate";
	$rows=$PdoMySQL->find($table,null,'*',null,null,null,null);
	return $rows;
}

/**
 * 添加资讯信息
 * @return string
 */
function addConsult(){
	$PdoMySQL=new PdoMySQL;
	$arr=$_POST;
	$arr['aTime'] = date("Y-m-d",time());
	if($PdoMySQL->add($arr,"bac_consult")){
		$mes="添加成功!<br/><a href='addconsult.php'>继续添加</a>|<a href='listconsult.php'>查看资讯分类列表</a>";
	}else{
		$mes="添加失败!<br/><a href='addconsult.php'>重新添加</a>";
	}
	return $mes;
}


/**
 * 得到所有的资讯信息
 * @return array
 */

function getConsultByPage($page,$pageSize=2,$whereStr){

	$whereStrs = "b.cid = c.id ".$whereStr;
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_consult b,bac_con_cate c";
    global $totalRows;
	$totalRows=count($PdoMySQL->find($tables,$whereStrs,'b.id id, b.title, b.content, b.author, b.aTime, c.id cId, c.cName',null,null,null,null));
	global $totalPage;
	$totalPage=ceil($totalRows/$pageSize);
	if($page<1||$page==null||!is_numeric($page)){
		$page=1;
	}

	if($page>=$totalPage)$page=$totalPage;
	$offset=($page-1)*$pageSize;

	if($offset < 0){
		$offset = 0;
	}

	$rows=$PdoMySQL->find($tables,$whereStrs,'b.id bId, b.title, b.content, b.author, b.aTime, c.id, c.cName',null,null,null,array($offset,$pageSize));
	return $rows;
}

/**
 * 得到指定ID的资讯分类信息
 * @return array
 */
function findConCateByCid($id){
	$sql = "select c.id cId,c.cName from bac_consult as b join bac_con_cate c on b.cId = c.id where b.id={$id}";
	$PdoMySQL=new PdoMySQL;
	$row=$PdoMySQL->getAll($sql);
	return $row;
}

/**
 * 得到指定ID的资讯信息
 * @return array
 */
function findConsultById($id){
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_consult";
	$row = $PdoMySQL->findById($tables, $id, 'id, cId, title, content, author, aTime');
	return $row;
}

/**
 * 编辑资讯信息
 * @param int $id
 * @return string
 */
function editConsult($id){
	$arr=$_POST;
	 $arr['aTime'] = date("Y-m-d H:i",time());

	$PdoMySQL=new PdoMySQL;
	$table = "bac_consult";

	if($PdoMySQL->update($arr,$table, "id={$id}",'','')){
		$mes="编辑成功!<br/><a href='listConsult.php'>查看资讯分类列表</a>";
	}else{
		$mes="编辑失败!<br/><a href='listConsult.php'>请重新修改</a>";
	}
	return $mes;
}

/**
 * 删除指定id资讯分类
 * @param int $id
 * @return string
 */
function delConsult($id){
	$PdoMySQL=new PdoMySQL;
	$table = "bac_consult";
	
	if($PdoMySQL->delete($table,"id={$id}")){
		$mes="删除成功!<br/><a href='listConsult.php'>查看资讯列表</a>";
	}else{
		$mes="删除失败!<br/><a href='listConsult.php'>请重新删除</a>";
	}
	return $mes;
}